package com.concurrency.example.singleton;

public class SingletonExample3 {
    private SingletonExample3() {

    }

    //volatile防止指令重排
    private volatile static SingletonExample3 instance = null;

    //双重检测机制
    public static SingletonExample3 getInstance() {
        if (instance == null) {
            synchronized (SingletonExample3.class) {
                if (instance == null) {
                    instance = new SingletonExample3();
                }
            }
        }
        return instance;
    }

    public static void main(String[] args) {
        System.out.println(getInstance().hashCode());
        System.out.println(getInstance().hashCode());
    }
}